package edu.uclmgmu.bacteriosec.aspects.java.io.ObjectOutputStream;

import java.io.ObjectOutputStream;

import edu.uclmgmu.bacteriosec.aspects.common.Store;

privileged aspect ObjectOutputStream_RemoveLastByte {

	void around(java.io.ObjectOutputStream caller, byte[] a, int b, int c) : target(caller) && args(a, b, c) &&  call(* ObjectOutputStream.write(byte[], int, int)) {
		int numberOfCurrentCall=Store.get(thisJoinPointStaticPart.getSignature());
		int numberOfCallInTurn=Store.getNumberOfCallInTurn();
		if (numberOfCurrentCall==numberOfCallInTurn) {
			Store.log(this.getClass().getCanonicalName(), numberOfCurrentCall, thisJoinPointStaticPart.getSignature());
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);
			byte[] theArray=new byte[a.length-1];
			for (int i=0; i<a.length-1; i++) {
				theArray[i]=a[i];
			}
			a=theArray;
			proceed(caller, a, b, c);
		} else {
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);
			proceed(caller, a, b, c);
		}
	}

		void around(java.io.ObjectOutputStream caller, byte[] a) : target(caller) && args(a) &&  call(* ObjectOutputStream.write(byte[])) {
		int numberOfCurrentCall=Store.get(thisJoinPointStaticPart.getSignature());
		int numberOfCallInTurn=Store.getNumberOfCallInTurn();
		if (numberOfCurrentCall==numberOfCallInTurn) {
			Store.log(this.getClass().getCanonicalName(), numberOfCurrentCall, thisJoinPointStaticPart.getSignature());
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);
			byte[] theArray=new byte[a.length-1];
			for (int i=0; i<a.length-1; i++) {
				theArray[i]=a[i];
			}
			a=theArray;
			proceed(caller, a);
		} else {
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);
			proceed(caller, a);
		}
	}

	
}
